package com.lagou.controller;

import com.lagou.vo.MachineStatus;
import com.lagou.vo.ResponseContent;
import com.lagou.vo.UserInfo;
import com.lagou.vo.UserMatchInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: wudong
 * @create: 2021-08-03 16:34
 **/
@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("loginSystem")
    @ResponseBody
    public ResponseContent loginSystem(String username, String password, HttpSession session, HttpServletRequest request) {
        // 合法用户，信息写入session，同时跳转到系统主页面
        if("admin".equals(username) && "admin".equals(password)) {
            System.out.println("合法用户");
            session.setAttribute(UserInfo.sessionKey,UserInfo.build(username));
            return ResponseContent.buildSuccessResponseContent(UserMatchInfo.buildFromSession(session,request));
        }else{
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            ResponseContent responseContent = ResponseContent.buildErrorResponseContent(401, "账户名密码错误");
            responseContent.setValue(MachineStatus.build(request,session));
            return responseContent;
        }
    }
}
